package com.ihighest.app.infox.adapter;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.R;

public class ReplyCommentAdapter extends BaseAdapter {
	private final Context context;
	private JSONArray array;
	private final LayoutInflater inflate;

	public ReplyCommentAdapter(Context context) {
		this.context = context;
		this.inflate = LayoutInflater.from(context);
	}

	public ReplyCommentAdapter(Context context, JSONArray array) {
		this.context = context;
		this.inflate = LayoutInflater.from(context);
		this.array = array;
	}

	@Override
	public int getCount() {
		return array == null ? 0 : array.length();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View contextview, ViewGroup group) {
		final ViewHolder holder;
		if (contextview == null) {
			holder = new ViewHolder();
			contextview = inflate.inflate(R.layout.infox_activity_comment_detail_replyitem, null);
			holder.ueserImg = (ImageView) contextview.findViewById(R.id.replyUserImg);
			holder.replyUserName = (TextView) contextview.findViewById(R.id.replyUserName);
			holder.replyDate = (TextView) contextview.findViewById(R.id.replyDate);
			holder.replyContent = (TextView) contextview.findViewById(R.id.replyContent);
			holder.replyThumpUpCount = (TextView) contextview.findViewById(R.id.replyThumpCount);
			holder.replyInmg = (ImageView) contextview.findViewById(R.id.replyThumpImg);
			holder.replyThump = (RelativeLayout) contextview.findViewById(R.id.replyThumpLayout);
			contextview.setTag(holder);
		} else {
			holder = (ViewHolder) contextview.getTag();
		}
		try {
			final String id = array.getJSONObject(position).getString("id");
			holder.replyUserName.setText(array.getJSONObject(position).getString("name"));
			holder.replyContent.setText(array.getJSONObject(position).getString("content"));
			holder.replyDate.setText(AppKit.timeToBeforeStr(array.getJSONObject(position).getLong("time")));
			final int thumpUpCount = array.getJSONObject(position).getInt("praises");
			holder.replyThumpUpCount.setText(array.getJSONObject(position).getInt("praises") + "");
			// Drawable drawable =
			// context.getResources().getDrawable(R.drawable.infox_person_no_icon);
			// DisplayImageOptions defaultOptions = new
			// DisplayImageOptions.Builder().showImageForEmptyUri(drawable).showImageOnFail(drawable)
			// .showImageOnLoading(drawable).cacheInMemory(true).cacheOnDisc(true).build();
			// ImageLoader.getInstance().displayImage(array.getJSONObject(position).getString("image"),
			// holder.ueserImg, defaultOptions);

			AppKit.SetUserImg(array.getJSONObject(position).getString("image"), holder.ueserImg);
			if (array.getJSONObject(position).getBoolean("isPraises")) {

				holder.replyInmg.setImageDrawable(context.getResources().getDrawable(R.drawable.infox_comment_thumbup_press));
				holder.replyThumpUpCount.setTag("2");

			} else {
				holder.replyThumpUpCount.setTag("1");
				holder.replyInmg.setImageDrawable(context.getResources().getDrawable(R.drawable.infox_comment_thumbup));
			}
			holder.replyThump.setOnClickListener(new OnClickListener() {

				private int inta;

				@Override
				public void onClick(View position) {
					String action = null;

					if (!TextUtils.isEmpty(holder.replyThumpUpCount.getTag().toString())) {
						if (holder.replyThumpUpCount.getTag().equals("1")) {
							action = "1";
							holder.replyThumpUpCount.setTag("2");
							inta = thumpUpCount + 1;
							holder.replyInmg.setImageDrawable(context.getResources().getDrawable(R.drawable.infox_comment_thumbup_press));
							holder.replyThumpUpCount.setText(inta + "");
						} else {
							action = "2";
							holder.replyThumpUpCount.setTag("1");
							if (inta >= 1) {

								holder.replyThumpUpCount.setText(inta - 1 + "");
							} else {
								holder.replyThumpUpCount.setText("0");
							}
							holder.replyInmg.setImageDrawable(context.getResources().getDrawable(R.drawable.infox_comment_thumbup));
						}
					} else {
						action = "1";
						holder.replyThumpUpCount.setTag("2");
						inta = thumpUpCount + 1;
						holder.replyInmg.setImageDrawable(context.getResources().getDrawable(R.drawable.infox_comment_thumbup_press));
						holder.replyThumpUpCount.setText(inta + "");
					}
					Api.get("comment/praise", ApiParam.create().addParam("id", id).addParam("type", "2").addParam("action", action),
							new ApiCallback(context, true) {

								@Override
								public void handleSuccess(ApiResult result) {

								}

								@Override
								public void handleFailure(Exception e, String msg) {
									// TODO Auto-generated method stub

								}
							}, context);
				}
			});

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return contextview;
	}

	class ViewHolder {
		ImageView ueserImg;
		TextView replyUserName;
		TextView replyDate;
		TextView replyContent;
		TextView replyThumpUpCount;
		ImageView replyInmg;
		RelativeLayout replyThump;
	}
}
